#pragma once
#include <iostream>
#include <unordered_map>
#include "Request&&Response.hpp"
#include "CreatRoom.hpp"




class ServerManager
{
public:
	ServerManager(){}
	void Init()
	{
		_servers.insert({ CreateRoom, std::make_pair(new ResponseCreateRoom(), new RequestCreateRoom()) });

	}

	bool Serialize(ServerType server_type, std::string* msg)
	{
		Json::Value root;
		root["server_type"] = server_type;
		Json::FastWriter writer;
		*msg = writer.write(root) + " " + *msg;

		return true;
	}
	bool Deserialize(const std::string& result)
	{
		Json::Value root;
		Json::Reader reader;
		reader.parse(result, root);
		ServerType server_type = (ServerType)(root["server_type"].asInt());
		int pos = result.find(' ');
		if (pos == std::string::npos)
			return false;
		std::string msg = result.substr(pos + 1);
		_servers[server_type].first->Deserialize(msg);

		return true;
	}

	void Destory()
	{
		delete _servers[CreateRoom].first;
		delete _servers[CreateRoom].second;
	}
	~ServerManager(){}
private:
	std::unordered_map <ServerType, std::pair<Response*, Request*>> _servers;

};